//
// Created on 2024/10/24.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef SUB_UNSUB_UV_QUEUE_H
#define SUB_UNSUB_UV_QUEUE_H
#include <functional>
#include "napi/native_api.h"
#include <js_native_api_types.h>
#include "uv.h"

class UvQueue final {
public:
    using NapiArgsGenerator = std::function<void(napi_env env, int& argc, napi_value* argv)>;
    using NapiCallbackGetter = std::function<napi_value(napi_env env)>;
    explicit UvQueue(napi_env env);
    ~UvQueue();

    napi_env GetEnv();
    void AsyncCall(NapiCallbackGetter getter, NapiArgsGenerator genArgs = NapiArgsGenerator());
private:
    static void Work(uv_work_t* work, int uvStatus);
    struct UvEntry {
        napi_env env;
        NapiCallbackGetter callback;
        NapiArgsGenerator args;
    };
    napi_env env_ = nullptr;
    uv_loop_s* loop_ = nullptr;
};

#endif //SUB_UNSUB_UV_QUEUE_H
